package org.javason.jsonrpc.impl;

import java.lang.reflect.InvocationTargetException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.javason.jsonrpc.JsonRpcMessageContext;
import org.javason.jsonrpc.impl.JsonRpcOperationInvoker.OperationAndParams;

public class JsonRpcSystemOperationInvoker extends JsonRpcOperationInvoker {
	
	public JsonRpcSystemOperationInvoker(JsonRpcService service) {
		super(new JsonRpcService(new JsonRpcSystem(service)));
	}
	
	public OperationAndParams getMatchingOperationAndParams(String methodName, JSONArray params, JsonRpcMessageContext msgCtx) {
		return super.getMatchingOperationAndParams(getSystemMethod(methodName), params, msgCtx);
	}
	
	public Object invokeMethod(String methodName, JSONArray params, JsonRpcMessageContext msgCtx) 
			throws NoSuchMethodException, IllegalArgumentException, InvocationTargetException, IllegalAccessException {
		return super.invokeMethod(getSystemMethod(methodName), params, msgCtx);
	}
	
	public Object invokeMethod(String methodName, JSONObject params, JsonRpcMessageContext msgCtx) 
			throws NoSuchMethodException, IllegalArgumentException, InvocationTargetException, IllegalAccessException {
		return super.invokeMethod(getSystemMethod(methodName), params, msgCtx);
	}

	
	private String getSystemMethod(String methodName) {
		return methodName.replaceFirst("system\\.", "");
	}
}
